﻿using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using Slqj.Common;
using System;

namespace Logan.Base.Common.Mail
{
    /// <summary>
    /// 邮件发送类
    /// author:huangjc 2008-9-10
    /// 说明：在使用过程中，如发现本基类有错误，请先在错误的方法前打label，并与黄炬灿联系更正错误。
    /// 例：
    /// //有误
    /// public void Send()
    /// {.....}
    /// </summary>
    /// <summary>
    /// Mail Clase
    /// Add by Liyx
    /// 2009-01-16
    /// </summary>
    public class SendMail
    {

        #region Class attribute
        private string mailFrom;
        private string mailTo;
        private string mailSubject;
        private string mailBody;

        private bool isBodyHTML=false;

        private string userName = Config.GetAppSetting("Mail_Address");
        private string password = Config.GetAppSetting("Mail_Password");
        private string smtpServer = Config.GetAppSetting("Mail_Server");

        private bool isSSL = bool.Parse(Config.GetAppSetting("Mail_IsSSL"));
        private static int ports = 0;
        bool istrue = int.TryParse(Config.GetAppSetting("Mail_Port"), out ports);


        /// <summary>
        /// 发信人
        /// </summary>
        public string MailFrom
        {
            get { return mailFrom; }
            set
            {
                mailFrom = value;
            }
        }
        /// <summary>
        /// 收信人
        /// </summary>
        public string MailTo
        {
            get { return mailTo; }
            set
            {
                mailTo = value;
            }
        }
        /// <summary>
        /// 主题
        /// </summary>
        public string MailSubject
        {
            get { return mailSubject; }
            set
            {
                mailSubject = value;
            }
        }
        /// <summary>
        /// 邮件内容
        /// </summary>
        public string MailBody
        {
            get { return mailBody; }
            set
            {
                mailBody = value;
            }
        }

        /// <summary>
        /// 是否以HTML发送内容
        /// </summary>
        public bool IsBodyHTML
        {
            get { return isBodyHTML; }
            set { isBodyHTML = value; }
        }
        #endregion

        #region Send
        /// <summary>
        /// 发送邮件
        /// </summary>
        public void Send()
        {
            string isPwd = Config.GetAppSetting("Mail_IsPassword");

            MailMessage myMail = new MailMessage(mailFrom, mailTo, mailSubject, mailBody);
            SmtpClient mySend = new SmtpClient(smtpServer);
            myMail.IsBodyHtml = IsBodyHTML;
            //mySend.Credentials = CredentialCache.DefaultNetworkCredentials;
            if (ports > 0)
            {
                mySend.Port = ports;
            }
            mySend.EnableSsl = isSSL;

            if (isPwd.ToLower().Equals("true"))
            {
                mySend.Credentials = new NetworkCredential(userName, password);
            }

            try
            {
                mySend.Send(myMail);
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 发送带一个附件的邮件
        /// </summary>
        /// <param name="filePath">附件文件路径</param>
        public void Send(string filePath)
        {
            MailMessage myMail = new MailMessage(mailFrom, mailTo, mailSubject, mailBody);
            Attachment myData = new Attachment(filePath, MediaTypeNames.Application.Octet);
            ContentDisposition myDisposition = myData.ContentDisposition;
            myDisposition.CreationDate = System.IO.File.GetCreationTime(filePath);
            myDisposition.ModificationDate = System.IO.File.GetLastWriteTime(filePath);
            myDisposition.ReadDate = System.IO.File.GetLastAccessTime(filePath);
            myMail.Attachments.Add(myData);
            SmtpClient mySend = new SmtpClient(smtpServer);
            //mySend.Credentials = CredentialCache.DefaultNetworkCredentials;
            if (ports > 0)
            {
                mySend.Port = ports;
            }
            mySend.EnableSsl = isSSL;
            mySend.Credentials = new NetworkCredential(userName, password);
            try
            {
                mySend.Send(myMail);
            }
            catch (System.Net.Mail.SmtpException ex)
            {

            }
            finally
            {
                myData.Dispose();
            }
        }

        /// <summary>
        ///发送有多个附件的邮件 
        /// </summary>
        /// <param name="filePathList">附件文件路径</param>
        public void Send(string[] filePathList)
        {
            MailMessage myMail = new MailMessage(mailFrom, mailTo, mailSubject, mailBody);
            for (int i = 0; i < filePathList.Length; i++)
            {
                Attachment myData = new Attachment(filePathList[i], MediaTypeNames.Application.Octet);
                ContentDisposition myDisposition = myData.ContentDisposition;
                myDisposition.CreationDate = System.IO.File.GetCreationTime(filePathList[0]);
                myDisposition.ModificationDate = System.IO.File.GetLastWriteTime(filePathList[0]);
                myDisposition.ReadDate = System.IO.File.GetLastAccessTime(filePathList[0]);
                myMail.Attachments.Add(myData);
            }
            SmtpClient mySend = new SmtpClient(smtpServer);
            //mySend.Credentials = CredentialCache.DefaultNetworkCredentials;
            if (ports > 0)
            {
                mySend.Port = ports;
            }
            mySend.EnableSsl = isSSL;
            mySend.Credentials = new NetworkCredential(userName, password);
            try
            {
                mySend.Send(myMail);
            }
            catch { }
            finally
            {
                myMail.Attachments.Dispose();
            }
        }
        #endregion

        #region Mass-mailing
        /// <summary>
        /// 群发邮件
        /// </summary>
        /// <param name="mailToList">邮件地址列表</param>
        public void SendAll(string[] mailToList)
        {
            for (int i = 0; i < mailToList.Length; i++)
            {
                MailTo = mailToList[i];
                Send();
            }
        }

        /// <summary>
        /// 群发带附件的邮件
        /// </summary>
        /// <param name="addressList">邮件地址列表</param>
        /// <param name="filePath">附件文件路径</param>
        public void SendAll(string[] mailToList, string filePath)
        {
            for (int i = 0; i < mailToList.Length; i++)
            {
                MailTo = mailToList[i];
                Send(filePath);
            }
        }
        #endregion

    }

}
